package com.opal.jx.service.impl;

import com.opal.consts.WSC;
import com.opal.domain.R;
import com.opal.jx.JxConst.JxConst;
import com.opal.jx.entity.JxAssessReception;
import com.opal.jx.entity.table.JxAssessPbb;
import com.opal.jx.entity.table.JxAssessPbbTotal;
import com.opal.jx.entity.table.JxEmp;
import com.opal.jx.exception.JxAssessException;
import com.opal.jx.mapper.mysql.JxPBBMapper;
import com.opal.jx.service.JxAssessCommonService;
import com.opal.jx.service.JxPBBService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class JxPBBServiceImpl implements JxPBBService {
    @Autowired
    private JxAssessCommonService jxAssessCommonService;
    @Autowired
    private JxPBBMapper jxPBBMapper;
    @Override
    public Map<String,Object> getTableDataByEmp(JxAssessReception jxAssessReception) {
        String pkDept = jxAssessReception.getPkDept();
        JxEmp jxEmp= new JxEmp();
        jxEmp.setPkDept(pkDept);
        List<JxEmp> jxEmps = jxAssessCommonService.selectEmpReturnLead(pkDept);

        Map<String,Object> result= new HashMap<>();
        Map<String, Object> zgDataMap = new HashMap<>();
        Map<String, Object> jyDataMap = new HashMap<>();
        zgDataMap.put("total",0 );
        jyDataMap.put("total",0 );
        List<JxEmp> zgDataList = new ArrayList<>();
        List<JxEmp> jyDataList = new ArrayList<>();
        List<Map> zgDataListFooter = new ArrayList<>();
        List<Map> jyDataListFooter = new ArrayList<>();
        Set<String> jyDataListFooterset = new HashSet();
        for (JxEmp jx :jxEmps){
            Long postId = jx.getPostId();
            if (postId == 21){
                zgDataList.add(jx);
            }else {
                jyDataList.add(jx);
                jyDataListFooterset.add(jx.getEmpZhuguan());
            }
        }
        zgDataMap.put("rows",zgDataList);
        jyDataMap.put("rows", jyDataList);
        //footer
        for (String s:jyDataListFooterset){
            Map<String,String> m= new HashMap<>();
            m.put("empCode",s);
            m.put("resultMoney", "0");
            jyDataListFooter.add(m);
        }
        Map<String,String> m= new HashMap<>();
        m.put("empCode","合计");
        m.put("resultMoney", "0");
        zgDataListFooter.add(m);
        jyDataListFooter.add(m);
        zgDataMap.put("footer", zgDataListFooter);
        jyDataMap.put("footer", jyDataListFooter);

        result.put("zgData", zgDataMap);
        result.put("jyData", jyDataMap);
        return result;
    }

    @Override
    public Object getTableDataByJxAssessPbb(JxAssessReception jxAssessReception) {
        Map<String,Object> result= new HashMap<>();
        Map<String, Object> jyDataMap = new HashMap<>();
        Map<String, Object> zgDataMap = new HashMap<>();

        Integer year = jxAssessReception.getYear();
        Integer month = jxAssessReception.getMonth();
        //获取主数据
        List<JxAssessPbb> jxAssessPbbs = jxPBBMapper.selectJxAssessPbbByYearAndMonth(year, month);
        //检验员
        List<JxAssessPbb> jy = jxAssessPbbs.stream().filter(a -> a.getAssessType() == 1 ? true : false).collect(Collectors.toList());
        //主管
        List<JxAssessPbb> zg = jxAssessPbbs.stream().filter(a -> a.getAssessType() == 2 ? true : false).collect(Collectors.toList());
        jyDataMap.put("rows", jy);
        zgDataMap.put("rows",zg);
        //获取footer数据
        List<JxAssessPbbTotal> footers=   jxPBBMapper.selectJxAssessByYearAndMonthFooter(year,month);
        List temp =new ArrayList();

        //检验员
        List<JxAssessPbbTotal> jyTotal = footers.stream().filter(a -> a.getAssessType() == 1 ? true : false).collect(Collectors.toList());
        for (JxAssessPbbTotal j:jyTotal){
            Map map=new HashMap();
            map.put("empCode", j.getRemarks());
            map.put("resultMoney", j.getTotalMoney());
            temp.add(map);
        }
        jyDataMap.put("footer", temp);
        jyDataMap.put("total", jyTotal.size());
        //主管
        List<JxAssessPbbTotal> zgTotal = footers.stream().filter(a -> a.getAssessType() == 2 ? true : false).collect(Collectors.toList());
        temp=new ArrayList();
        for (JxAssessPbbTotal j:zgTotal){
            Map map=new HashMap();
            map.put("empCode", j.getRemarks());
            map.put("resultMoney", j.getTotalMoney());
            temp.add(map);
        }
        zgDataMap.put("footer", temp);
        zgDataMap.put("total", jyTotal.size());

        result.put("jyData", jyDataMap);
        result.put("zgData", zgDataMap);
        return result;
    }

    @Override
    @Transactional("mysqlTransactionManager")
    public R insert(JxAssessReception jxAssessReception) {
        try {
            //插入品保部绩效数据
            //获取数据
            List<JxAssessPbb> jxAssessPbbs = jxAssessReception.getJxAssessPbbs();
            Integer assessYear = jxAssessPbbs.get(0).getAssessYear();
            Integer assessMonth = jxAssessPbbs.get(0).getAssessMonth();
            //插入数据前先删除当前月的数据
            jxPBBMapper.deleteByYearAndMonth(assessYear,assessMonth);
            jxPBBMapper.deleteTotalByYearAndMonth(assessYear, assessMonth);
            //插入数据
            jxAssessPbbs.forEach(a->jxPBBMapper.insert(a));
            //插入品保部统计数据
            List<JxAssessPbbTotal> jxAssessPbbTotals = jxAssessReception.getJxAssessPbbTotals();
            jxAssessPbbTotals.forEach(a->jxPBBMapper.insertTotal(a));
        }catch (Exception e){
            throw new JxAssessException(WSC.REQUEST_ERROR,JxConst.SUBMIT_EFEATED);
        }


        return new R(WSC.RESTPONSE_OK,JxConst.SUBMIT_SUCCESS);
    }
}
